package com.ksc.wordcount.service.client;

import com.ksc.wordcount.service.UrlTopNAppRequest;
import com.ksc.wordcount.service.UrlTopNAppResponse;
import com.ksc.wordcount.service.UrlTopNResult;
import com.ksc.wordcount.service.UrlTopNService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import java.util.List;

public class UrlTopNAppRequestClient {

    public static void main(String[] args) {
        System.out.println("thrift客户端启动....");
        TTransport transport = null;
        try {
            transport = new TSocket( "localhost", 5151);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            UrlTopNService.Client client = new UrlTopNService.Client(protocol);
            transport.open();
            UrlTopNAppRequest urlTopNAppRequest = new UrlTopNAppRequest();

            String applicationId="apllication_1235";
            urlTopNAppRequest.setTopN(10);
            urlTopNAppRequest.setApplicationId(applicationId);
            urlTopNAppRequest.setInputPath("/root/kingsoft/wordcount/input");
            urlTopNAppRequest.setOuputPath("/root/kingsoft/wordcount/output");
            urlTopNAppRequest.setNumReduceTasks(2);
            urlTopNAppRequest.setSplitSize(1024);

            UrlTopNAppResponse result = client.submitApp(urlTopNAppRequest);
            System.out.println("submitApp result is "+result);
            UrlTopNAppResponse appStatus = client.getAppStatus(applicationId);
            System.out.println("appStatus is "+appStatus);

            List<UrlTopNResult> topNAppResult = client.getTopNAppResult(applicationId);
            System.out.println("topNAppResult is "+topNAppResult);

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }
}
